/**
 * Author: Andrew Shore
 * Date Last Modified: April 19, 2009
 * CSCE 146: Homework 07, MultiList.java
 * 
 * Description:
 * This class implements a MultiList using an array of Priority 
 * Queues. Process records are inserted into the array based
 * on its process queue and is sorted by priority. The dump
 * method cycles through the array and removes process records
 * in round-robin order.
 */
public class MultiList {
	private PQ[] processQueue;
	private final int DEFAULT_SIZE = 8;
	private int size;
	private static final String classLabel = "MultiList: ";
	
	/**
	 * Default Constructor
	 */
	public MultiList(){
		setProcessQueue(new PQ[DEFAULT_SIZE]);
		for(int i = 0; i < processQueue.length; i++){
			processQueue[i] = new PQ();
		}//for(int i = 0; i < processQueue.length; i++)
		size = 0;
	}
	
	/** 
	 * Accessors
	 */
	public PQ[] getProcessQueue() {
		return processQueue;
	}
	public int getSize() {
		return size;
	}
	
	/**
	 * Mutators
	 */
	public void setProcessQueue(PQ[] processQueue) {
		this.processQueue = processQueue;
	}
	public void setSize(int what){
		this.size = what;
	}


	/**
	 * Method to add a record to the MultiList based on it's processQueue,
	 * calls PQ insert method
	 * 
	 * Parameters: rec - record being inserted
	 */
	public void insert(ProcessRecord rec){
		int Q = rec.getProcessQ()-1;
			processQueue[Q].insert(rec);
			this.setSize(this.getSize()+1);
	}
	
	/**
	 * Method to remove records from the MultiList. Cycles through the 
	 * array of PQ's and keeps removing in sequential order until all 
	 * records are removed
	 */
	public void dump(){
				
		while(this.getSize() != 0){
			for(int i = 0; i < processQueue.length; i++){
				if(processQueue[i].getList().isEmpty()){
					FileUtils.logFile.println(classLabel + " q: " + (i + 1) + ", empty");
				}else{
				
					//removes process record and prints the information
					FileUtils.logFile.println(classLabel + " q: " + (i + 1) + ", record	"
						 + processQueue[i].remove().toString());
			
					this.setSize(this.getSize()-1);	
			
			}
		}//for(int i = 0; i < processQueue.length; i++)	
			FileUtils.logFile.flush();
		}//while(this.getSize() != 0)
		
	}

	/**
	 * Returns a String of the array of Process Queue's
	 */
	public String toString(){
		String s = "";
		for(int i = 0; i < processQueue.length; i++){
			s += processQueue[i].toString() + " \n";
		}//for(int i = 0; i < processQueue.length; i++)
		return s;
	}

	
}
